<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import serachGift from '@/components/serachGift/index.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    activityArr:[] as Array<any>,
  })
  interface formQuert {
    id:number
    sort:number
    gift_id:number
    pool_level:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    sort: [{ required: true, trigger: 'blur', message: '请输入排序值' }],
    gift_id: [{ required: true, trigger: 'blur', message: '请选择礼物' }],
  }
  const handleSubmit = async()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      if(form.id) {
        // await luckDrawApi.wishDefenderAddGift(form).then(()=>{
        //   submitSet = true
        // }).catch(()=>{
        //   loading.close()
        // })
      } else {
        await luckDrawApi.wishDefenderSendAddGift(form).then(()=>{
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        ToastSuccess(`${action.title}成功`)
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const getActivityList = async()=>{
    const loading = Toastloading()
    const res = await luckDrawApi.get_defender_draw({page:1,limit:100})
    const { data=[] } = res.data
    action.activityArr = data
    loading.close()
  }
  const openDialog = async(query:any = {})=> {
    const { id=0, desc='', Defender_id='', pool_level=0 } = query
    if(!pool_level) return ToastWarning('缺少 pool_level')
    Object.assign(form,{
      id,desc,Defender_id,pool_level
    })
    await getActivityList()
    action.title = id?'编辑奖励礼物':'新增奖励礼物'
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="10vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="40%"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="礼物" prop="gift_id">
          <serachGift :gift_type="1" v-model:gift_id="form.gift_id" />
        </el-form-item>

        <el-form-item :label-width="formLabelWidth" label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序"/>
        </el-form-item>
<!--        <el-form-item :label-width="formLabelWidth" label="游戏手数" prop="sort">-->
<!--          <el-input v-model="form.sort" placeholder="请输入游戏手数"/>-->
<!--        </el-form-item>-->
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
